﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace Lswweb.Controls
{
    public class FloatTipTextBox : TextBox,INamingContainer
    {
        private string floatTipDivId = "floatTipDiv";

        #region 子控件创建
        protected override void CreateChildControls()
        {
            HtmlGenericControl control = new HtmlGenericControl("div");
            control.ID = floatTipDivId;
            control.Attributes.Add("onclick", "clearTimeout($('#" + this.ClientID + "').focus()[0].constructor);");

            control.Attributes.Add("style", "position:absolute;background:#fff;display:none;" + FloatTipStyle);
            if (FloatTip != null)
                FloatTip.InstantiateIn(control);
            Controls.Add(control);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            this.Attributes.Add("onblur", "this.constructor=setTimeout(function(){$('#" + GetFloatTipDivClientId + "').hide();},500)");
            this.Attributes.Add("onfocus", "clearTimeout($('#" + this.ClientID + "').focus()[0].constructor);$('#" + GetFloatTipDivClientId + "').show();");
            this.RenderBeginTag(writer);
            if (this.TextMode == TextBoxMode.MultiLine)
            {
                HttpUtility.HtmlEncode(this.Text, writer);
            }
            this.RenderContents(writer);
            this.RenderEndTag(writer);
        }

        #endregion

        [Browsable(true)]
        [TemplateContainer(typeof(FloatTipTextBox))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public ITemplate FloatTip { get; set; }

        public string FloatTipStyle{get;set;}

        private string GetFloatTipDivClientId
        {
            get { return this.ClientID + "_" + floatTipDivId; }
        }

    }
}
